package com.wangrui.sso1.clienta.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * Sa-Token-SSO Client端 Controller
 * @author click33
 */
@RestController
public class SsoClientController {


    @SaCheckLogin
    @GetMapping("/getUserInfo")
    public String getUserInfo(){
        return "<a href='#'>userinfo</a>";
    }

    // SSO-Client端：首页
    @RequestMapping("/")
    public String index() {
        String authUrl = SaSsoManager.getClientConfig().splicingAuthUrl();
        String solUrl = SaSsoManager.getClientConfig().splicingSloUrl();
        String str = "<h2>Sa-Token SSO-Client A 应用端</h2>" +
                "<p>当前会话是否登录：" + StpUtil.isLogin() + "</p>" +
                "<p><a href=\"" + authUrl + "?mode=simple&redirect=" + URLEncoder.encode("http://sso.com:9001", StandardCharsets.UTF_8) + " \">登录</a> " +
                "<a href=\"" + solUrl +  "?back=http%3A%2F%2Fclient1.sso.com%3A9001\">注销</a> </p>";
        return str;
    }

    // 全局异常拦截
    @ExceptionHandler
    public SaResult handlerException(Exception e) {
        e.printStackTrace();
        return SaResult.error(e.getMessage());
    }

}
